﻿using System;
using System.Threading;
using System.Windows;

namespace Mari.SmartHotel.Controls
{
    public static class SHMessageBox
    {
        private static SHMessageBoxWindow mb;

        public static void CloseCurrent()
        {
            if (mb != null)
            {
                mb.Close();
            }
        }

        public static void ShowAsync(string format, params object[] args)
        {
            //Show(string.Format(format, args), Properties.Resources.SmartClientName, MessageBoxButton.OK, MessageBoxImage.None, null, null, false);
        }

        public static void ShowAsync(string text)
        {
            //Show(text, Properties.Resources.SmartClientName, MessageBoxButton.OK, MessageBoxImage.None, null, null, false);
        }

        public static MessageBoxResult Show(MessageBoxButton button, MessageBoxImage image, string format, params object[] args)
        {
            return MessageBoxResult.OK;
            //return Show(string.Format(format, args), Properties.Resources.SmartClientName, button, image, null, null, true);
        }

        public static MessageBoxResult Show(string text)
        {
            return MessageBoxResult.OK;
            //return Show(text, Properties.Resources.SmartClientName, MessageBoxButton.OK, MessageBoxImage.Information, null, null, true);
        }

        public static MessageBoxResult Show(string text, string caption)
        {
            return Show(text, caption, MessageBoxButton.OK, MessageBoxImage.Information, null, true);
        }

        public static MessageBoxResult Show(string text, string caption, MessageBoxButton button)
        {
            return Show(text, caption, button, MessageBoxImage.Information, null, true);
        }

        public static MessageBoxResult Show(string text, string caption, MessageBoxButton button, MessageBoxImage image)
        {
            return Show(text, caption, button, image, null, true);
        }

        //public static MessageBoxResult Show(string text, string caption, MessageBoxButton button, MessageBoxImage image, string idTelegram)
        //{
        //    return Show(text, caption, button, image, null, idTelegram, true);
        //}

        public static MessageBoxResult Show(string text, string caption, MessageBoxButton button, Exception exception)
        {
            return Show(text, caption, button, MessageBoxImage.Error, exception, true);
        }


        private static MessageBoxResult Show(string text, string caption, MessageBoxButton button, MessageBoxImage image, Exception exception, bool dialogMode)
        {
            if (Application.Current.Dispatcher.Thread != Thread.CurrentThread)
            {
                return (MessageBoxResult)Application.Current.Dispatcher.Invoke(new Func<MessageBoxResult>(() => SHMessageBox.Show(text, caption, button, image, exception, dialogMode)));
            }

            //SmartClientWindow.RestoreDefaultCursor();

            try
            {
                //if (Configuration.SmartClient186.Instance.AdvancedUIMode)
                //{
                mb = new SHMessageBoxWindow();
                mb.IsDialog = dialogMode;
                mb.Message = text;
                mb.Caption = caption;
                mb.MessageBox186Button = button;
                mb.MessageBox186Image = image;
                mb.Exception = exception;
                //mb.IdTelegram = idTelegram;

                if (dialogMode)
                {
                    mb.ShowDialog();
                }
                else
                {
                    mb.Topmost = true;
                    mb.Show();
                }
                return mb.MessageBox186Result;
                //}
                //else
                //{
                //    if (exception != null)
                //    {
                //        text = string.Format(CultureInfo.InvariantCulture, "{0}\r\n{1}", text, exception.ToString());
                //    }
                //    return MessageBox.Show(text, caption, button, image);
                //}
            }
            catch (Exception)
            {
                return MessageBox.Show(string.Format("{0}", text));
            }
        }

    }
}
